package com.mall.service;

import java.util.List;


import com.mall.ibatis.bean.ICell;
import com.mall.ibatis.bean.JQGrid;
import com.mall.ibatis.bean.Method;
import com.mall.ibatis.bean.Operator;
import com.mall.ibatis.bean.ReturnMessage;
import com.mall.ibatis.persistence.DaoConfig;
import com.mall.ibatis.persistence.iface.MethodDAO;
import com.ibatis.dao.client.DaoManager;

public class MethodService {
	private static final MethodService instance = new MethodService();
	
	private DaoManager daoManager = DaoConfig.getDaomanager();
	
	private MethodDAO methodDAO = null;
	
	private MethodService(){
		methodDAO  = (MethodDAO) daoManager.getDao(MethodDAO.class);
	}
	
	public static MethodService getInstance(){
		return instance;
	}
	//方法的更新
	public ReturnMessage updateMethod(Method method){
		ReturnMessage message = null;
		try{
			int i = methodDAO.updateMethod(method);
			if(i > 0){
				message = new ReturnMessage("1");
			}else{
				message = new ReturnMessage("0","更新失败");
			}
		}catch(Exception e){
			message = new ReturnMessage("0","更新失败" + e.getMessage());
		}
		return message;
	}
	//方法的更新
	public ReturnMessage deleteMethod(String id){
		ReturnMessage message = null;
		try{
			int i = methodDAO.deleteMethod(id);
			if(i > 0){
				message = new ReturnMessage("1");
			}else{
				message = new ReturnMessage("0","删除失败");
			}
		}catch(Exception e){
			message = new ReturnMessage("0","删除失败" + e.getMessage());
		}
		return message;
	}
	
	//方法的增加
	public ReturnMessage addMethod(Method method){
		ReturnMessage message = null;
		try{
			String str = methodDAO.addMethod(method);
			if(str != null){
				message = new ReturnMessage("1");
			}else{
				message = new ReturnMessage("0","新增失败");
			}
		}catch(Exception e){
			message = new ReturnMessage("0","新增失败" + e.getMessage());
		}
		return message;
	}
	//方法列表的加载.在功能jf_funmenu数据库表中查询出所有的方法
	public JQGrid selectAllMethod(){
		List<Method> list = methodDAO.selectAllMethod();
		JQGrid jqGrid = new JQGrid();
		
		jqGrid.setPage(1);
		jqGrid.setRecord(list.size());
		jqGrid.setTotal(1);
		
		List<ICell> rowsList = jqGrid.getRowXML();
		
		
		for(Method method :list)
		{
			rowsList.add(method);
		}
		return jqGrid;
	}

	//方法的Id查找有哪些用户
	public JQGrid selectUserList(String methodId) {
		List<Operator> list = methodDAO.selectUserList(methodId);
		JQGrid jqGrid = new JQGrid();
		
		jqGrid.setPage(1);
		jqGrid.setRecord(list.size());
		jqGrid.setTotal(1);
		
		List<ICell> rowsList = jqGrid.getRowXML();
		
		
		for(Operator o :list)
		{
			rowsList.add(o);
		}
		return jqGrid;
	}

	//获取放法的Id号
	public String getMethodId() {
		return methodDAO.getMethodId();
	}
}
